package com.gleason.gt.service;

import java.io.IOException;
import java.util.Random;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
//import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;

public class MusicService extends Service {
	private static final int PLAY_ID = 1;
	@Override
	public IBinder onBind(Intent intent) {
		return new MusicServiceImpl();
	}
	public class MusicServiceImpl extends IMusicService.Stub {
		private MediaPlayer mMediaPlayer = new MediaPlayer();
	    @SuppressWarnings("unused")
		private Long lastSong = (long)0.0;
	    private Long currentSong = (long)0.0;
		private String title = null;
	    
		
		public void skipLeft(){
//	    	playSong(lastSong);
	    }
		public void pause(){
	    	if(mMediaPlayer.isPlaying()){
	    		mMediaPlayer.pause();
	    	}
	    }
		public void play(){
	    	if(!mMediaPlayer.isPlaying() && currentSong != 0){
	    		mMediaPlayer.start();
	    	}
	    	else{
	    		skipRight();
	    	}
	    }
	    @SuppressWarnings("unused")
		private Long getRandomSongId(){
	    	ContentResolver contentResolver = getContentResolver();
	    	Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
	    	Cursor cursor = contentResolver.query(uri, null, null, null, null);
	    	Integer test2 = cursor.getCount();
	    	Random randomGenerator = new Random();
	    	Integer randomNumber = randomGenerator.nextInt(test2.intValue());
	    	cursor.moveToPosition(randomNumber);
	    	int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
	    	currentSong = cursor.getLong(idColumn);
	    	int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
	    	title=cursor.getString(titleColumn);
	    	return currentSong;
	    }
	    private void playSong(){
	    	try{
	    		mMediaPlayer.reset();
	    		mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
	        	mMediaPlayer.setDataSource(getString(com.gleason.gt.R.string.stream_url));
	        	mMediaPlayer.prepare();
	        	mMediaPlayer.start();
	        	notifyPlay();
	    	}
	    	catch(IOException iox){
	    		//TODO: add exception handling
	    	}
	    	
	    }

	    public void skipRight(){
	    	lastSong = currentSong;
	    	playSong();
	    }
	    public String getTitle(){
	    	return title;
	    }
	    @Override
	    protected void finalize() throws Throwable {
	        try {
	        	mMediaPlayer.release();
	        	mMediaPlayer = null;        // close open files
	        } finally {
	            super.finalize();
	        }
	    }
	}
    public void notifyPlay(){
    	String ns = Context.NOTIFICATION_SERVICE;
    	NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
    	int icon = com.gleason.gt.R.drawable.ic_stat_example;
    	CharSequence tickerText = "Play";
    	long when = System.currentTimeMillis();

    	Notification notification = new Notification(icon, tickerText, when);
    	Context context = getApplicationContext();
    	CharSequence contentTitle = "Radio is Playing";
    	CharSequence contentText = "Welcome to my radio station!";
    	Intent notificationIntent = new Intent(this, MusicService.class);
    	PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    	notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    	

    	mNotificationManager.notify(PLAY_ID, notification);
    }
    
}
